package com.sky.controller.user;

import com.sky.dto.AddressBookAddDOT;
import com.sky.dto.AddressBookSetDefaultDTO;
import com.sky.dto.AddressBookUpdateDOT;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "地址管理")
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;

    @GetMapping("/list")
    public Result<List<AddressBook>> list() {
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }

    /**
     * 新增
     *
     * @param dot
     * @return
     */
    @PostMapping
    public Result<Void> add(@RequestBody AddressBookAddDOT dot) {
        addressBookService.add(dot);
        return Result.success();
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> selectById(@PathVariable String id) {
        AddressBook addressBook = addressBookService.selectById(id);
        return Result.success(addressBook);
    }

    /**
     * 修改
     */
    @PutMapping
    public Result<Void> update(@RequestBody AddressBookUpdateDOT dto) {
        addressBookService.update(dto);
        return Result.success();
    }

    /**
     * 默认
     */
    @PutMapping("/default")
    public Result<Void> setDefault(@RequestBody AddressBookSetDefaultDTO dto) {
        addressBookService.setDefault(dto);
        return Result.success();
    }

    /**
     * 根据id删除
     */
    @DeleteMapping
    public Result<Void> delete(@RequestParam("id") Long id) {
        addressBookService.deleteById(id);
        return Result.success();
    }
}
